/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: cjy.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月02日 星期五 14时02分14秒
***************************************************/

#include "cjy.h"
// 通过键盘输入数据，在内存新建一个链表
STU *creat_list(void){
    STU *head = NULL;
    head = (STU*)malloc(sizeof(STU));
    memset(head,0,sizeof(STU));
    STU *p = head;
    int count = 0;
    int flag = 0;
    int n;
    printf("请输入想存几个值:");
    scanf("%d",&n);
    while(flag != 1) {
        count ++;
        STU *point = malloc(sizeof(STU));
        memset(point,0,sizeof(STU));
        printf("请输入id:");
        scanf("%d",&point->id);
        printf("请输入name:");
        scanf("%s",point->name);
        printf("请输入age:");
        scanf("%d",&point->age);
        p->next = point;
        point->next = NULL;
        p = point;
        if(count == n) {
            flag = 1;
        }
    }
    return head;
}
// 查看链表内容
void display(STU *head){
    if(head == NULL) {
        return;
        }
        //辅助指针变量
        STU *p = head->next;
        while(p != NULL) {
            // 输出id
            printf("id:%d\n",p->id);
            // 输出name
            printf("name:%s\n",p->name);
            // 输出name
            printf("age:%d\n",p->age);
            p = p->next;
        }
}
// 释放链表
void delLinkMem(STU*head)
{
    STU*cur = NULL;
    STU*temp = head;
    while(temp!=NULL)
    {
        cur = temp->next;
        free(temp);
        temp = NULL;
        temp = cur;
    }
}
// 将链表内容保存到数据表中
void save(STU *head){
    char sql[100] = {0};
    MYSQL *res = NULL;
    MYSQL *con = NULL;
    con = mysql_init(NULL);
    res = mysql_real_connect(con,"localhost","root","123","cjy",0,NULL,0);
    if(res == NULL){
        printf("%s\n",mysql_error(con));
        exit(-1);
    }
    int ret = 0;
    STU *p = head->next;
    while(p != NULL) {
        sprintf(sql,"insert into student values(%d,'%s',%d)",p->id,p->name,p->age);
        ret = mysql_query(con,sql);
        if(ret != 0) {
            printf("%s\n",mysql_error(con));
            exit(-1);
        }
        p = p->next;
    }
    mysql_close(con);
}

// 拉sql数据在内存新建一个链表
STU *sql_creat_list(void){
    char sql[100] = {"select * from student"};
    MYSQL *res = NULL,*con = NULL;
    int ret = 0;
    con = mysql_init(NULL);
    res = mysql_real_connect(con,"localhost","root","123","cjy",0,NULL,0);
    if(res == NULL)
    {
        printf("%s\n",mysql_error(con));
        exit(-1);
    }
    // 上传sql
    ret = mysql_query(con,sql);
    if(ret != 0)
    {
        printf("%s\n",mysql_error(con));
        mysql_close(con);
        exit(-1);
    }
    MYSQL_RES *res1 = NULL;
    MYSQL_ROW row = NULL;
    res1 = mysql_store_result(con);
    if(res1 == NULL)
    {
        printf("%s\n",mysql_error(con));
        mysql_close(con);
        exit(-1);
    }
    STU *head = NULL;
    head = (STU*)malloc(sizeof(STU));
    memset(head,0,sizeof(STU));
    STU *p = head;
    row = mysql_fetch_row(res1);	
    while(row != NULL) {
        STU *point = malloc(sizeof(STU));
        memset(point,0,sizeof(STU));

        sscanf(row[0],"%d",&point->id);
	sscanf(row[1],"%s",point->name);
	sscanf(row[2],"%d",&point->age);

        row = mysql_fetch_row(res1);  
        p->next = point;
        point->next = NULL;
        p = point;
        
    }
    mysql_free_result(res1);

    mysql_close(con);

    return head;
}

